﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCNHibernate.Models;
using MVC.BLL;
using MVC.Common;

namespace MVCNHibernate.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Employee()
        {
            EmployeeModel model = new EmployeeModel();
            //GetEmployeeDetails(new JQueryDataTableParamModel());
            return View(model);
        }

        [HttpPost]
        public ActionResult Employee(EmployeeModel model)
        {
            model.ErrMsg = string.Empty;
            model.SuccessMsg = string.Empty;
            if (ModelState.IsValid)
            {
                if (model.btnClicked == "Save")
                {
                    var empName = model.EmpName;
                    var empAddress = model.Address;
                    int deptId = 1;
                    EmployeeBLL bll = new EmployeeBLL();
                    try
                    {
                        bll.Save(empName, empAddress, deptId);
                    }
                    catch (Exception)
                    {
                        model.ErrMsg = "Error Occured While Saving";
                    }
                    model.SuccessMsg = "Saved Successfully";
                }
            }
            else
            {
                model.ErrMsg = "Error:";
            }
            return View(model);
        }

        public JsonResult GetEmployeeDetails(JQueryDataTableParamModel param)
        {
            List<EmployeeModel> repResults = null;
            EmployeeBLL service = new EmployeeBLL();
            try
            {
                IList<EmployeeDTO> dtos = service.GetAllEmployeeDetail();
                repResults = MapDTOtoModel(dtos);
            }
            catch (Exception ex)
            {
                //TODO: logger and err msg
            }
            var results = from r in repResults
                          select new[] 
                          {
                              r.EmpId.ToString()
                              , r.EmpName
                              , r.Address
                          };

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = results.Count(),
                iTotalDisplayRecords = param.iDisplayLength,
                aaData = results
            }, JsonRequestBehavior.AllowGet);
        }

        private List<EmployeeModel> MapDTOtoModel(IList<EmployeeDTO> dtos)
        {
            List<EmployeeModel> models = new List<EmployeeModel>();
            if (dtos != null && dtos.Count > 0)
            {
                foreach (var item in dtos)
                {
                    EmployeeModel model = new EmployeeModel();
                    models.Add(new EmployeeModel() { 
                    EmpId=item.Id,
                    EmpName=item.Name,
                    Address = item.Address
                    });
                }
            }
            return models;
        }
    }
}
